package itemCF.step5;

import itemCF.step4.Mapper4;
import itemCF.step4.Reducer4;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.net.URI;

/**
 * @author legolas
 * @date 2020/3/20 上午12:20
 */
public class Step5Job {
    private static String inPath = "/itemCF/step4_output/";
    private static String outPath = "/itemCF/step5_output/";
    private static String cache = "/itemCF/step1_output/***";
    private static String hdfs = "hdfs://hadoopmaster:9000";

    public int run() {
        try {
            //创建Job需要的配置参数
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", hdfs);
            //创建一个job
            Job job = Job.getInstance(conf);

            //设置缓存
            job.addCacheFile(new URI(cache + "#itemUserScore5"));

            job.setJarByClass(Step5Job.class);
            //指定输入路径
            FileInputFormat.setInputPaths(job, new Path(inPath));
            FileOutputFormat.setOutputPath(job, new Path(outPath));
            //指定map相关代码
            job.setMapperClass(Mapper5.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(Text.class);

            //指定reduce相关代码
            job.setReducerClass(Reducer5.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);

            //提交job
            boolean status = job.waitForCompletion(true);
            return status ? 1 : -1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static void main(String[] args) {
        try {
            int result;
            result = new Step5Job().run();
            if (result == 1) {
                System.out.println("step5运行成功……");
            } else {
                System.out.println("step5运行失败……");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
